package com.qeegoo.b2bautozityp.moudle.statis.viewmodel;

import android.databinding.ObservableField;

import com.github.mikephil.charting.charts.LineChart;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.BR;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.base.BaseAdapter;
import com.qeegoo.b2bautozityp.moudle.statis.model.ListOrderListBean;
import com.qeegoo.b2bautozityp.moudle.statis.model.StatisticsBean;
import com.qeegoo.b2bautozityp.utils.ChartUtils;

import java.util.ArrayList;

import base.lib.util.ToastUtils;

/**
 * Created by lining on 2017/9/19.
 */

public class StatisViewModel {

    private RequestApi mRequestApi;
    public ObservableField<String> orderAmount = new ObservableField<>();
    public ObservableField<String> profitAmount = new ObservableField<>();
    public ObservableField<String> saleCount = new ObservableField<>();
    public ObservableField<String> underCount = new ObservableField<>();
    public ObservableField<String> noStockCount = new ObservableField<>();
    public ObservableField<String> dataText = new ObservableField<>();
    private BaseAdapter<ListOrderListBean> mAdapter;
    private ArrayList<ListOrderListBean> mItemViewModels = new ArrayList<>();

    public StatisViewModel(RequestApi requestApi) {
        mRequestApi = requestApi;
        mAdapter = new BaseAdapter<>(BR.viewModel,R.layout.adapter_statis_order_item, mItemViewModels);
    }
    public BaseAdapter<ListOrderListBean> getAdapter() {
        return mAdapter;
    }

    public void loadHttp(LineChart chart){

//        ChartUtils.initChart(chart);
//        ChartUtils.notifyDataSetChanged(chart, getData(), ChartUtils.dayValue);
        mRequestApi.orderStatistics().compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<StatisticsBean>() {
                    @Override
                    public void onNext(StatisticsBean statisticsBean) {
                        orderAmount.set(statisticsBean.orderAmount);
                        profitAmount.set("订单总收益 (元) "+statisticsBean.profitAmount);
                        saleCount.set(statisticsBean.saleCount);
                        underCount.set(statisticsBean.underCount);
                        noStockCount.set(statisticsBean.noStockCount);
//                        dataText.set(statisticsBean.listOrderStatistic.get(0).dateText+"-"+statisticsBean.listOrderStatistic.get(statisticsBean.listOrderStatistic.size()-1).dateText);
                        dataText.set("("+statisticsBean.listOrderStatistic.get(0).getDataM()+"-"+statisticsBean.listOrderStatistic.get(statisticsBean.listOrderStatistic.size()-1).getDataM()+")");
                        ChartUtils.notifyDataSetChanged(chart, statisticsBean.listOrderStatistic);
                        mItemViewModels.clear();
                        mItemViewModels.addAll(statisticsBean.listOrderList);
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }


    public ReplyCommand saleCommand = new ReplyCommand(()->{
        ToastUtils.showToast("在售");
    });

    public ReplyCommand underCommand = new ReplyCommand(()->{
        ToastUtils.showToast("下架");
    });

    public ReplyCommand noStockCommand = new ReplyCommand(()->{
        ToastUtils.showToast("售罄");
    });
}
